package com.lwt.mvc.handler;

import com.github.pagehelper.PageInfo;
import com.lwt.constant.TongchouConstant;
import com.lwt.entity.Admin;
import com.lwt.service.api.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

/**
 * @Author Li Weitong
 * @Date 2021/1/15 10:10
 */
@Controller
public class AdminHandler {

    @Autowired
    private AdminService adminService;

    /**
     * 登录成功重定向到/admin/to/main/page
     * @param loginAcct
     * @param userPswd
     * @param session
     * @return
     */
    @RequestMapping("/admin/do/login")
    public String doLogin(
            @RequestParam("loginAcct") String loginAcct,
            @RequestParam("userPswd") String userPswd,
            HttpSession session
        ){ // 可以传一个Amdin，但易与数据库查出的对象混淆，故使用@RequestParam
        Admin admin = adminService.getAdminByLoginAcct(loginAcct,userPswd);
        session.setAttribute(TongchouConstant.ATTR_NAME_LOGIN_ADMIN,admin); // 将登录成功返回的admin对象存入session域
//        为了避免跳转到后台主页再刷新浏览器导致重复提交登录表单，重定向到目标页面
        return "redirect:/admin/to/main/page"; // 登录成功通过springMVC的view-controller配置前往后台主页
    }

    /**
     * 注销 退出登录
     * @param session
     * @return
     */
    @RequestMapping("/admin/do/logout")
    public String doLogout(HttpSession session){
        session.invalidate(); // 强制session失效
        return "redirect:/admin/to/login/page";
    }

    /**
     * 普通请求实现分页，带搜索参数和不带的两种情况适配
     * @param keyword
     * @param pageNum
     * @param pageSize
     * @param modelMap
     * @return
     */
    @RequestMapping("/admin/get/page")
    public String getPageInfo(
            @RequestParam(value = "keyword",defaultValue = "") String keyword, // 请求时未携带keyword时是空字符串
            @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize",defaultValue = "6") Integer pageSize, // 每页显示6条
            ModelMap modelMap
            ){
        PageInfo<Admin> pageInfo = adminService.getPageInfo(keyword, pageNum, pageSize);
        modelMap.addAttribute(TongchouConstant.ATTR_NAME_PAGE_INFO,pageInfo);
        return "admin-page";
    }

    /**
     * 普通请求 实现单条删除
     * @param adminId
     * @param pageNum
     * @return
     */
    @RequestMapping("/admin/remove/{adminId}/{pageNum}")
    public String remove(
            @PathVariable("adminId") Integer adminId,
            @PathVariable("pageNum") Integer pageNum
            ){
        adminService.remove(adminId);
            return "redirect:/admin/get/page?pageNum="+pageNum; // 保持原本所在页面和查询关键字参数
    }

    @RequestMapping("/admin/save")
    public String save(Admin admin){
        adminService.saveAdmin(admin);

        return "redirect:/admin/get/page?pageNum="+Integer.MAX_VALUE;
    }

    @RequestMapping("/admin/to/edit/page")
    public String toEditPage(
            @RequestParam("adminId") Integer adminId,
            @RequestParam("pageNum") Integer pageNum,
            ModelMap modelMap
            ){
        Admin admin = adminService.getAdminById(adminId);
        modelMap.addAttribute("admin",admin);
        return "admin-edit";
    }

    @RequestMapping("/admin/update")
    public String update(
            Admin admin,
            @RequestParam("pageNum") Integer pageNum
            ){
        adminService.update(admin);
        return "redirect:/admin/get/page?pageNum="+pageNum;
    }
}
